import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pickers/more_pickers/init_data.dart';
import 'package:flutter_pickers/more_pickers/route/single_picker_route.dart';
import 'package:flutter_pickers/pickers.dart';
import 'package:flutter_pickers/style/default_style.dart';
import 'package:flutter_pickers/style/picker_style.dart';
import 'package:flutter_pickers/time_picker/model/date_item_model.dart';
import 'package:flutter_pickers/time_picker/model/date_mode.dart';
import 'package:flutter_pickers/time_picker/model/pduration.dart';
import 'package:flutter_pickers/time_picker/model/suffix.dart';
import 'package:flutter_pickers/time_picker/route/date_picker_route.dart';

/// 参考 https://github.com/longer96/flutter_pickers#%E5%86%85%E7%BD%AE%E6%95%B0%E6%8D%AE

class PickerWidget extends Pickers {

  /// 单列选择器
  /// * [context] BuildContext|上下文
  /// * [data] List<Map>|选择器呈现的数据
  /// * [selectData] String|默认选中的数据
  /// * [suffix] String|后缀（如：42分）
  /// * [pickerStyle] PickerStyle|选择器呈现的风格
  /// * [onChanged] SingleCallback|取消回调
  /// * [onConfirm] SingleCallback|确定回调
  /// * [fieldNames] String|需要筛选的字段名称（例如：{ code:1,name:kell },需要匹配name字段）
  static void showNewSinglePicker(
      BuildContext context, {
    @required dynamic data,
    dynamic selectData,
    String suffix,
    PickerStyle pickerStyle,
    SingleCallback onChanged,
    SingleCallback onConfirm,
    String fieldNames = 'name'
  }) {

    // 用于将data的数据拆解并存储指定的字段
    List newList = [];
    assert(data != null, 'params: data can not be null');
    assert((data is List) || (data is PickerDataType), 'params : data must List or PickerDataType');

    if (pickerStyle == null) {
      pickerStyle = DefaultPickerStyle();
    }
    if (pickerStyle.context == null) {
      pickerStyle.context = context;
    }
    //print(data[0].name);
    // 检测是否传入的时List<Map>类型数据
    // true：调整数据格式
    if(data is List){
      data.forEach((e) => newList.add(e[fieldNames]));
    } else {
      newList = data;
    }



    Navigator.push(
        context,
        SinglePickerRoute(
          data: newList,
          suffix: suffix,
          selectData: selectData,
          pickerStyle: pickerStyle,
          onChanged: onChanged,
          onConfirm: (p) {
            Map result = data.firstWhere((e) => e[fieldNames] == p);
            onConfirm(result);
          },
          theme: Theme.of(context),
          barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
        ));
  }


  /// 时间选择器
  /// * [context] BuildContext|上下文
  /// * [mode] DateMode|时间格式
  /// * [selectDate] PDuration|默认选中的时间
  /// * [maxDate] PDuration|时间最大值
  /// * [minDate] PDuration|时间最小值
  /// * [suffix] Suffix|后缀（如：42分）
  /// * [pickerStyle] PickerStyle|选择器呈现的风格
  /// * [onChanged] SingleCallback|取消回调
  /// * [onConfirm] SingleCallback|确定回调
  static void showNewDatePicker(
    BuildContext context, {
      DateMode mode: DateMode.YMD,
      PDuration selectDate,
      PDuration maxDate,
      PDuration minDate,
      Suffix suffix,
      PickerStyle pickerStyle,
      DateCallback onChanged,
      DateCallback onConfirm
  }) {

    if (pickerStyle == null) {
      pickerStyle = DefaultPickerStyle();
    }
    if (pickerStyle.context == null) {
      pickerStyle.context = context;
    }

    if (selectDate == null) selectDate = PDuration.now();
    if (suffix == null) suffix = Suffix.normal();

    // 解析是否有对应数据
    DateItemModel dateItemModel = DateItemModel.parse(mode);

    if (maxDate == null) maxDate = PDuration(year: 2100);
    if (minDate == null) minDate = PDuration(year: 1900);

    if ((dateItemModel.day || dateItemModel.year)) {
      if (intEmpty(selectDate.year)) {
        print('picker  Tip >>> initDate未设置years，默认设置为now().year');
        selectDate.year = DateTime.now().year;
      }

      /// 如果有年item ，必须限制
      if (intEmpty(maxDate.year)) maxDate.year = 2100;
      if (intEmpty(minDate.year)) minDate.year = 1900;

      // print('longer >>> ${minDate.year}');

      if (dateItemModel.month || dateItemModel.day) {
        assert(minDate.year > 1582, 'min Date Year must > 1582');
      }
    }

    Navigator.push(
        context,
        DatePickerRoute(
          mode: mode,
          initDate: selectDate,
          maxDate: maxDate,
          minDate: minDate,
          suffix: suffix,
          pickerStyle: pickerStyle,
          onChanged: onChanged,
          onConfirm: onConfirm,
          // theme: Theme.of(context, shadowThemeOnly: true),
          theme: Theme.of(context),
          barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
        ));

  }

}